package icu.shaoyayu.common.feign.filter;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author shaoyayu
 * @date 2022/2/16
 * @apiNote
 */
@Configuration
public class RequestUserNameInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //老请求 获取老请求的cookie信息
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null){
            String userName = requestAttributes.getRequest().getHeader("UserName");
            //同步到新请求中
            if (userName!=null){
                requestTemplate.header("UserName",userName);
            }
        }
    }
}
